package com.gs.synedocket.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.db.dto.Base64PhotoDto;
import com.gs.synedocket.db.entity.Docket;
import com.gs.synedocket.db.entity.DocketPhoto;
import com.gs.synedocket.db.entity.UserTruck;
import com.gs.synedocket.model.SyncUploadModel;
import com.gs.synedocket.model.UtilModel;
import com.gs.synedocket.model.listener.OnResultListener;
import com.gs.synedocket.view.ISyncUpload;

import java.util.ArrayList;
import java.util.List;

public class SyncUploadPresenter extends BasePresenter<ISyncUpload> {
    private final SyncUploadModel syncModel = new SyncUploadModel();
    private final UtilModel utilModel = new UtilModel();

    public void syncUserTruck(List<UserTruck> list, int position) {
        syncModel.syncUserTruck(list.get(position).getTruckId(), new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onSyncUserTruckSuccess((ApiResultData<String>) apiResult, list, position);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onSyncUserTruckFails(e);
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }

    public void syncDocket(Docket bean) {
        List<Docket> list = new ArrayList<>();
        list.add(bean);
        syncModel.syncDocket(list, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onSyncDocketSuccess((ApiResultData<String>) apiResult, bean);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onSyncDocketFails(e);
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }

    public void upload(Docket bean, String photo, int flag) {
        upload(bean, photo, null, 0, flag);
    }

    public void upload(Docket bean, String photo, List<DocketPhoto> photos, int position, int flag) {
        if (getView() != null) {
            utilModel.upload(photo, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().onUploadFileSuccess((ApiResultData<String>) aipResult, bean, photos, position, flag);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().onUploadFileFails(e);
                    }
                }
            }, new TypeToken<ApiResultData<String>>() {
            }.getType());
        }
    }

    public void uploadBase64(Docket docket, String photo, boolean isTakePhoto, int flag) {
        if (getView() != null) {
            Base64PhotoDto bean = new Base64PhotoDto();
            bean.setBase64(photo);
            bean.setExeName(AllConstants.BASE64_NAME);
            utilModel.uploadBase64(bean, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().onUploadBase64Success((ApiResultData<String>) aipResult, docket, isTakePhoto, flag);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().onUploadBase64Fails(e);
                    }
                }
            }, new TypeToken<ApiResultData<String>>() {
            }.getType());
        }
    }
}
